package net.airtheva.uniqueTask4;

import net.airtheva.uniqueTask4.fragment.MediaFragment;
import net.airtheva.uniqueTask4.fragment.PlayingFragment;
import net.airtheva.uniqueTask4.fragment.PlaylistFragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.viewpagerindicator.TitlePageIndicator;

public class MainActivity extends FragmentActivity {

	public static final String TAG = "MainActivity";

	public static final int POSITION_MEDIA_FRAGMENT = 0;
	public static final int POSITION_PLAYLIST_FRAGMENT = 1;
	public static final int POSITION_PLAYING_FRAGMENT = 2;
	public static final int POSITION_LYRIC_FRAGMENT = 3;

	public static final int TIMEOUT_BACK_TWICE = 2000;

	class _FragmentPagerAdapter extends FragmentPagerAdapter {

		public _FragmentPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			switch (position) {
			case POSITION_MEDIA_FRAGMENT:
				return MediaFragment;
			case POSITION_PLAYLIST_FRAGMENT:
				return PlaylistFragment;
			case POSITION_PLAYING_FRAGMENT:
				return PlayingFragment;
			default:
				return null;
			}

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 3;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			switch (position) {
			case POSITION_MEDIA_FRAGMENT:
				return "Media";
			case POSITION_PLAYLIST_FRAGMENT:
				return "Playlist";
			case POSITION_PLAYING_FRAGMENT:
				return "Playing";
			case POSITION_LYRIC_FRAGMENT:
				return "Lyric";
			default:
				return null;
			}
		}

	}

	public PlayerService Service;
	
	public MediaFragment MediaFragment;
	public PlaylistFragment PlaylistFragment;
	public PlayingFragment PlayingFragment;

	_FragmentPagerAdapter mFragmentPagerAdapter;
	TitlePageIndicator mViewPagerIndicator;
	ViewPager mViewPager;

	public PlayerServiceConnection mPlayerServiceConnection;

	Handler mHandler;
	boolean mIsExiting;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Utils.Initialize(this);

		mFragmentPagerAdapter = new _FragmentPagerAdapter(
				getSupportFragmentManager());
		mViewPagerIndicator = (TitlePageIndicator) findViewById(R.id.activity_main_viewPagerIndicator);
		mViewPager = (ViewPager) findViewById(R.id.activity_main_viewPager);

		mViewPager.setAdapter(mFragmentPagerAdapter);
		mViewPagerIndicator.setViewPager(mViewPager);

		MediaFragment = new MediaFragment();
		PlaylistFragment = new PlaylistFragment();
		PlayingFragment = new PlayingFragment();

		mPlayerServiceConnection = new PlayerServiceConnection();
		
		mPlayerServiceConnection.SetOnServiceConnectedListener(new PlayerServiceConnection.OnServiceConnectedListener() {
			
			@Override
			public void OnServiceConnected(IBinder service) {
				// TODO Auto-generated method stub
				Service = ((PlayerService._Binder) service).GetServive();
				
				int status = Service.GetStatus();
				
				Log.d(TAG, status + "");
				
				switch(status) {
				case PlayerService.STATUS_PLAYING:
					PageTo(POSITION_PLAYING_FRAGMENT);
					break;
				case PlayerService.STATUS_PAUSING:
					PageTo(POSITION_PLAYLIST_FRAGMENT);
					break;
				}
				
			}
		});
		
		mHandler = new Handler();

		bindService(new Intent(this, PlayerService.class), mPlayerServiceConnection, BIND_AUTO_CREATE);
		
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:

			if (!mIsExiting) {

				mIsExiting = true;

				Toast.makeText(this, "Press BACK again to exit.",
						Toast.LENGTH_SHORT).show();

				mHandler.postDelayed(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						mIsExiting = false;
					}

				}, TIMEOUT_BACK_TWICE);

			} else {
				finish();
			}

			return true;
		default:
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		
		switch(item.getItemId()) {
		case R.id.activity_main_exit:
			System.exit(0);
			break;
		}
		
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub

		if (mPlayerServiceConnection != null) {
			unbindService(mPlayerServiceConnection);
			mPlayerServiceConnection = null;
		}

		super.onDestroy();
	}
	
	public void PageTo(int position) {
		
		/*
		switch(position) {
		case POSITION_MEDIA_FRAGMENT:
			MediaFragment.setUserVisibleHint(true);
			break;
		case POSITION_PLAYLIST_FRAGMENT:
			PlaylistFragment.setUserVisibleHint(true);
			break;
		case POSITION_PLAYING_FRAGMENT:
			PlayingFragment.setUserVisibleHint(true);
			break;
		case POSITION_LYRIC_FRAGMENT:
			LyricFragment.setUserVisibleHint(true);
			break;
		}
		*/
		
		mViewPager.setCurrentItem(position, true);
		
	}

}
